package cn.bdqn.controller;

import cn.bdqn.domain.Replenish;
import cn.bdqn.service.ReplenishService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 补单控制层
 * @author dkj
 */
@RestController
public class ReplenishController {

    /**
     * 创建日志记录器
     */
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ReplenishService replenishService;


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/replenish/query/{id}")
    public Result queryByPrimaryKey(@PathVariable("id") Integer id){

        try {
            logger.info("进入方法了，参数:id={}",id);
            //根据id查询
            Replenish replenish = replenishService.queryByPrimaryKey(id);
            //返回结果
            return ResultGenerator.genSuccessResult(replenish);
        }catch (Exception e){
            logger.error("报错了:"+e.getMessage());
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }


    /**
     * 根据发货单号和订单状态查询
     * @param currentPage 当前页
     * @param pageSize 显示数量
     * @param sendFreightNumber  发货单号
     * @param transportState 补货单状态
     * @return
     */
    @GetMapping("/replenish/query/{currentPage}/{pageSize}/{sendFreightNumber}/{transportState}")
    public Result selectBySendNumberAndTransportState(@PathVariable("currentPage") Integer currentPage,
                                                      @PathVariable("pageSize") Integer pageSize,
                                                      @PathVariable("sendFreightNumber") String sendFreightNumber,
                                                      @PathVariable("transportState") Integer transportState){
        try {
            logger.info("进入方法了,参数：" +
                    "currentPage={},pageSize={},sendFreightNumber={}, transportState={}",currentPage,pageSize,sendFreightNumber,transportState);
            //查询
            Page page = replenishService.queryBySendNumberAndTransportState(currentPage,
                    pageSize, sendFreightNumber, transportState);
            //返回结果
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了:"+e.getMessage());
            return ResultGenerator.genFailResult("查询失败");
        }
    }


    /**
     * 新增
     * @param replenish
     */
    @PostMapping("/replenish/save/")
    public Result saveReplenish(@RequestBody Replenish replenish){

        try {
            logger.info("进入了方法,参数replenish={}",replenish);
            //新增
            Replenish replenish1 = replenishService.saveReplenish(replenish);
            //返回结果
            return ResultGenerator.genSuccessResult(replenish1);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了"+e.getMessage());
            return ResultGenerator.genFailResult("新增失败");
        }
    }


    /**
     * 修改
     * @param replenish
     */
    @PutMapping("/replenish/update/")
    public Result updateReplenish(Replenish replenish){
        try {
            logger.info("进入了方法,参数replenish={}",replenish);
            //修改
            replenishService.updateReplenish(replenish);
            //返回结果
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了"+e.getMessage());
            return ResultGenerator.genFailResult("修改失败");
        }
    }
}
